<?php 

namespace app\common\shop;

use app\common\entity\User;
use think\Db;
use think\Model;
use traits\model\SoftDelete;

// 签到记录
class SignInLogModel extends model {
    //use SoftDelete;//开启了软删除
    //protected  = 'del_time';
    // 完整数据表名
    protected $table = 'sign_in_log';
    public $week = [
        '0'=>[
            'name'=>'周一',
            'display'=>0,
        ],
        '1'=>[
            'name'=>'周二',
            'display'=>0,
        ],
        '2'=>[
            'name'=>'周三',
            'display'=>0,
        ],
        '3'=>[
            'name'=>'周四',
            'display'=>0,
        ],
        '4'=>[
            'name'=>'周五',
            'display'=>0,
        ],
        '5'=>[
            'name'=>'周六',
            'display'=>0,
        ],
        '6'=>[
            'name'=>'周日',
            'display'=>0,
        ],
    ];
    /**
     * [建表]
     * @return
     */
    public static function createTable(){
        return Db::query("CREATE TABLE If Not Exists `sign_in_log` (
             `id` int(11) NOT NULL AUTO_INCREMENT,
             `uid` int(11) NOT NULL COMMENT '会员id',
             `number` decimal(22,2) NOT NULL DEFAULT '0' COMMENT '得到的福币数量',
             `createtime` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '签到时间',
             PRIMARY KEY (`id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='签到记录表'");
    }
 	/**
     * [添加]
     * @return
    */
    public static function add($data){
    	$data['createtime'] = time();
    	return self::insertGetId($data);
    }
    /**
     * [今天是否已经签到]
     * @param
     * @return
     */
    public static function checkTodaySignIn( $uid ){
        $todayTime = mktime(0,0,0,date('m'),date('d'),date('Y'));
        $info = self::where('uid',$uid)->where('createtime','>',$todayTime)->value('id');
        if($info) return true;// 已经签到
        return false;// 没有签到
    }

    /**
     * [根据时间判断是否签到]
     * @param
     * @return
     */
    public static function checkTimeSignIn($uid,$time=0,$endtime=0){
        $info = self::where('uid',$uid)->where('createtime','>=',$time)->where('createtime','<=',$endtime)->value('id');
        if($info) return true;// 已经签到
        return false;// 没有签到
    }
    /**
     * [累计签到福币]
     * @param
     * @return
     */
    public static function sumNumber($uid,$time = 0){
        return SignInLogModel::where('uid',$uid)->where('createtime','>',$time)->sum('number');
    }
    /**
     * [累计签到福币]
     * @param
     * @return
     */
    public static function addSignBless($uid,$number){
        return User::where('id',$uid)->setInc('signbless',$number);
    }
}